package com.lannuokeji.biz.items.controller;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lannuokeji.base.AController;
import com.lannuokeji.base.facade.PageRequest;
import com.lannuokeji.biz.items.entity.Device;
import com.lannuokeji.biz.items.entity.DeviceType;
import com.lannuokeji.biz.items.service.DeviceTypeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 设备分类 前端控制器
 * </p>
 *
 * @author lannuoTeck
 * @since 2018-03-19
 */
@Api(description="设备类型管理")
@RestController
@RequestMapping("/items/deviceType")
public class DeviceTypeController extends AController {
	
	@Resource
	DeviceTypeService deviceTypeService;
	
	@ApiOperation(value = "查询列表")
	@GetMapping()
	public Object getList() {
		List<DeviceType> l = deviceTypeService.selectList(new EntityWrapper<>());
		return setSuccessContent(l);
	}
	
	@ApiOperation(value = "查询列表")
	@GetMapping("/page")
	public Object getList(PageRequest pageRequest, Device entity) {
		Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());
		EntityWrapper<DeviceType> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("status", 1);
		page = deviceTypeService.selectPage(page, entityWrapper);
		return setSuccessContent(page);
	}

	@ApiOperation(value = "添加设备")
	@PostMapping()
	public Object create(HttpServletRequest request, @RequestBody DeviceType entity) {
		entity.setCreateTime(new Date());
		boolean ret = deviceTypeService.insertAllColumn(entity);
		if (ret) {
			return setSuccessContent("添加成功！");
		} else {
			return setErrorContent("添加失败！");
		}
	}

	@ApiOperation(value = "根据id查询设备")
	@GetMapping("/{id}")
	public Object get(HttpServletRequest request,
			@ApiParam(value = "设备id", required = true) @PathVariable Integer id) {
		DeviceType items = deviceTypeService.selectById(id);
		return setSuccessContent(items);
	}

	@ApiOperation(value = "删除设备信息")
	@DeleteMapping("/{id}")
	public Object del(HttpServletRequest request,
			@ApiParam(value = "设备id", required = true) @PathVariable Integer id) {
		if (deviceTypeService.deleteById(id)) {
			return setSuccessContent("删除成功");
		} else {
			return setErrorContent("删除失败");
		}
	}

	@ApiOperation(value = "修改设备信息")
	@PutMapping()
	public Object update(HttpServletRequest request, @RequestBody DeviceType entity) {
		if (deviceTypeService.updateById(entity)) {
			return setSuccessContent("修改成功！");
		} else {
			return setErrorContent("修改失败！");
		}
	}
}

